package com.my.ext.service.impl;

import java.util.List;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.my.ext.entity.Admin;
import com.my.ext.entity.Group;
import com.my.ext.entity.Org;
import com.my.ext.mapper.GroupMapper;
import com.my.ext.mapper.OrgMapper;
import com.my.ext.service.IGroupService;

@Service
public class GroupService implements IGroupService{

	@Autowired
	OrgMapper orgmapper;
	
	@Autowired
	GroupMapper groupMapper;
	
	@Override
	public PageInfo<Group> queryAllGroup(int page, int size) {
		PageHelper.startPage(page, size);
		Admin admin = (Admin)SecurityUtils.getSubject().getPrincipal();
		Org org = orgmapper.findByAdmin(admin);
		List<Group> list = groupMapper.findAll(org);
		PageInfo<Group> info = new PageInfo<>(list);
		return info;
	}

	@Override
	public int addGroup(Group group) {
		Admin admin = (Admin)SecurityUtils.getSubject().getPrincipal();
		Org org = orgmapper.findByAdmin(admin);
		group.setOrg(org);
		return groupMapper.saveGroup(group);
	}

	@Override
	public PageInfo<Group> queryGroupsByOrgid(int id) {
		PageHelper.startPage(1, 20);
		Org org = new Org();
		org.setId(id);
		List<Group> list = groupMapper.findAll(org);
		PageInfo<Group> info = new PageInfo<>(list);
		return info;
	}

	@Override
	public void modify(int id, String openid) {
		Group g = groupMapper.findMy(openid);
		g.getNode().setId(g.getNode().getId() + 1);
		groupMapper.modifyGroup(g);
	}
	
	

	
	
}
